
package com.seekting.study;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class UDPStudy {

    public static void main(String[] args) {
        new Thread(new Runnable() {

            @Override
            public void run() {

                socket();
            }
        }).start();
        new Thread(new Runnable() {

            @Override
            public void run() {
                udpServer();
            }
        }).start();

        udp();
    }

    private static void udpServer() {
        try {
            DatagramSocket server = new DatagramSocket(8820);
            byte[] recByte = new byte[100];
            DatagramPacket datagramPacket = new DatagramPacket(recByte, recByte.length);
            server.receive(datagramPacket);
            String str = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
            System.out.println("server" + str);
            InetAddress inetAddress = datagramPacket.getAddress();
            String ip = InetAddress.getLocalHost().getHostAddress();

            byte[] sendByte = ip.getBytes();
            DatagramPacket sendDatagramPacket = new DatagramPacket(sendByte, sendByte.length,
                    inetAddress,
                    datagramPacket.getPort());
            server.send(sendDatagramPacket);
            server.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void socket() {
        try {
            ServerSocket serverSocket = new ServerSocket(8820);
            Socket s = serverSocket.accept();

            DataOutputStream dataOutputStream = new DataOutputStream(s.getOutputStream());
            dataOutputStream.writeUTF("xxxxx");
            System.out.println("xie le");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void udp() {
        DatagramSocket dgSocket;
        try {
            dgSocket = new DatagramSocket(8810);

            byte[] request = "你好，我是客户端1234".getBytes();
            InetSocketAddress destAddress = new InetSocketAddress("255.255.255.255", 8820); // 目标广播地址+目标端口
            DatagramPacket dgPacket = new DatagramPacket(request, request.length, destAddress); // 数据包
            dgSocket.send(dgPacket); // 发送查找服务器请求UDP包
            byte[] response = new byte[20]; // 响应的字节数据缓冲区
            dgPacket = new DatagramPacket(response, 20, destAddress);
            dgSocket.receive(dgPacket);
            int length = dgPacket.getLength();
            String str = new String(response, 0, length);
            System.out.println(str);
            // 如下代码忽略，收到的数据放在response数组，转字符串再处
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
